package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.templates.L1Hierarch;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class NpcHierarchTable {
  private static final Log _log = LogFactory.getLog(NpcHierarchTable.class);
  
  private static NpcHierarchTable _instance;
  
  private static final HashMap<Integer, L1Hierarch> _powerMap = new HashMap<>();
  
  public static NpcHierarchTable get() {
    if (_instance == null)
      _instance = new NpcHierarchTable(); 
    return _instance;
  }
  
  private NpcHierarchTable() {
    load();
  }
  
  private void load() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM `w_随身祭司`");
      rs = pstm.executeQuery();
      while (rs.next()) {
        int npc_id = rs.getInt("npc_id");
        String skill_id = rs.getString("skill_id").replaceAll(" ", "");
        String skill_mp = rs.getString("skill_mp").replaceAll(" ", "");
        int[] SkillId = null;
        if (skill_id != null && !skill_id.equals("")) {
          String[] set2 = skill_id.split(",");
          SkillId = new int[set2.length];
          int i = 0;
          while (i < set2.length) {
            SkillId[i] = Integer.parseInt(set2[i]);
            i++;
          } 
        } 
        int[] SkillMp = null;
        if (skill_mp != null && !skill_mp.equals("")) {
          String[] set3 = skill_mp.split(",");
          SkillMp = new int[set3.length];
          int j = 0;
          while (j < set3.length) {
            SkillMp[j] = Integer.parseInt(set3[j]);
            j++;
          } 
        } 
        L1Hierarch hierarch = new L1Hierarch();
        hierarch.setSkillId(SkillId);
        hierarch.setSkillMp(SkillMp);
        _powerMap.put(Integer.valueOf(npc_id), hierarch);
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } catch (Exception e2) {
      _log.error(e2.getLocalizedMessage(), e2);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
    _log.info("载入祭司能力资料数量: " + _powerMap.size() + "(" + timer.get() + "ms)");
  }
  
  public L1Hierarch getHierarch(int key) {
    if (_powerMap.containsKey(Integer.valueOf(key)))
      return _powerMap.get(Integer.valueOf(key)); 
    return null;
  }
}
